/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views.Students;

import Controllers.Controller_Enrolment;
import Models.CoursesTemp;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author Cristea
 */
public class FrmEnrollInCourses extends javax.swing.JFrame {
    Controller_Enrolment _controller;
    List<CoursesTemp> _availableCourses;
    List<CoursesTemp> _selectedCourses;
    /**
     * Creates new form FrmEnrollInCourses
     */
    public FrmEnrollInCourses(Controller_Enrolment controller) {
        initComponents();
        SetLookAndFeel();
        _controller = controller;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        availableCoursesJList = new javax.swing.JList();
        availableCoursesJLabel = new javax.swing.JLabel();
        chooseCoursesButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        chosenCoursesJList = new javax.swing.JList();
        chosenCoursesJLabel = new javax.swing.JLabel();
        SunmitCoursesJButton = new javax.swing.JButton();
        removeCoursesJButton = new javax.swing.JButton();

        setTitle("Enroll in courses");

        availableCoursesJList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(availableCoursesJList);

        availableCoursesJLabel.setText("Available Courses");

        chooseCoursesButton.setText("Choose");
        chooseCoursesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseCoursesButtonActionPerformed(evt);
            }
        });

        chosenCoursesJList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(chosenCoursesJList);

        chosenCoursesJLabel.setText("Chosen Courses");

        SunmitCoursesJButton.setText("Submit");
        SunmitCoursesJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SunmitCoursesJButtonActionPerformed(evt);
            }
        });

        removeCoursesJButton.setText("Remove");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(availableCoursesJLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 216, Short.MAX_VALUE)
                .addComponent(chosenCoursesJLabel)
                .addGap(54, 54, 54))
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(removeCoursesJButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(chooseCoursesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
            .addGroup(layout.createSequentialGroup()
                .addGap(112, 112, 112)
                .addComponent(SunmitCoursesJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(138, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(availableCoursesJLabel)
                            .addComponent(chosenCoursesJLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(jScrollPane2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addComponent(chooseCoursesButton)
                        .addGap(47, 47, 47)
                        .addComponent(removeCoursesJButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(SunmitCoursesJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void SetAvailableCourses(List<CoursesTemp> courses)
    {
        DefaultListModel list = GetListModel(courses);
        this.availableCoursesJList.setModel(list);
    }
    
    public void SetSelectedCourses(List<CoursesTemp> courses)
    {
        DefaultListModel list = GetListModel(courses);
        this.chosenCoursesJList.setModel(list);
    }
    
    private DefaultListModel<CoursesTemp> GetListModel(List<CoursesTemp> list)
    {
       DefaultListModel<CoursesTemp> toReturn = new DefaultListModel();
       for(Iterator<CoursesTemp> it = list.iterator(); it.hasNext(); )
       {
           CoursesTemp course = it.next();
           toReturn.addElement(course);
       }
       return toReturn;
    }
    
    private void chooseCoursesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseCoursesButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_chooseCoursesButtonActionPerformed

    private void SunmitCoursesJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SunmitCoursesJButtonActionPerformed
        _controller.Submit(_selectedCourses);
    }//GEN-LAST:event_SunmitCoursesJButtonActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
    private void SetLookAndFeel()
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmEnrollInCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmEnrollInCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmEnrollInCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmEnrollInCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton SunmitCoursesJButton;
    private javax.swing.JLabel availableCoursesJLabel;
    private javax.swing.JList availableCoursesJList;
    private javax.swing.JButton chooseCoursesButton;
    private javax.swing.JLabel chosenCoursesJLabel;
    private javax.swing.JList chosenCoursesJList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton removeCoursesJButton;
    // End of variables declaration//GEN-END:variables
}
